package junior.util.date;

import junior.util.NumberUtils;

/**
 * @author bo.wu
 * @CreateDate 2014年8月12日
 */
public class TimeUtils {
    public final static long TIME_DAY = 1000 * 60 * 60 * 24;
    public final static long TIME_HOUR = 1000 * 60 * 60;
    public final static long TIME_MINUTE = 1000 * 60;
    public final static long TIME_SECOND = 1000;
    
    public static String toText(long spentTimes) {
        if (spentTimes == 0L) return "";
        
        if (spentTimes >= TIME_DAY) {
            long remain = spentTimes % TIME_DAY;
            return spentTimes / TIME_DAY + "d" + (remain > 0 ? "," + toText(remain) : "");
        } else if (spentTimes >= TIME_HOUR) {
            long remain = spentTimes % TIME_HOUR;
            return spentTimes / TIME_HOUR + "h" + (remain > 0 ? "," + toText(remain) : "");
        } else if (spentTimes >= TIME_MINUTE) {
            long remain = spentTimes % TIME_MINUTE;
            return spentTimes / TIME_MINUTE + "m" + (remain > 0 ? "," + toText(remain) : "");
        } else if (spentTimes >= TIME_SECOND) {
            long remain = spentTimes % TIME_SECOND;
            return spentTimes / TIME_SECOND + "s" + (remain > 0 ? "," + toText(remain) : "");
        } else {
            return spentTimes + new String(new char[]{'m', 's'});
        }
    }
	
	public static String getSpentTimeText(long spentTimes) {
		if(spentTimes < 1000) {
			return spentTimes + "ms";
		}
		
		double times = spentTimes / 1000D;
		if(times < 60) {
			return NumberUtils.floor(times, 3) + "s";
		}
		
		times = times / 60D;
		if(times < 60) {
			return NumberUtils.floor(times, 3) + "m";
		}
		
		times = times / 60D;
		if(times < 24) {
			return NumberUtils.floor(times, 3) + "h";
		}

		times = times / 24D;
		return NumberUtils.floor(times, 3) + "d";
	}
	
	 public static String toText2Min(long spentTimes) {
	        if (spentTimes == 0L) return "";
	        
	        if (spentTimes >= TIME_DAY) {
	            long remain = spentTimes % TIME_DAY;
	            return spentTimes / TIME_DAY + "d" + (remain > 0 ? "," + toText2Min(remain) : "");
	        } else if (spentTimes >= TIME_HOUR) {
	            long remain = spentTimes % TIME_HOUR;
	            return spentTimes / TIME_HOUR + "h" + (remain > 0 ? "," + toText2Min(remain) : "");
	        } else {
	            return spentTimes / TIME_MINUTE + "m";
	        }
	    }
	
}
